/*
 *  This class is part of WorkTimeCalculator Android App
 *  Author: Domenico Pio Novelli
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.dpndeveloper.wtc.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.widget.SimpleAdapter;

import com.dpndeveloper.wtc.R;
import com.dpndeveloper.wtc.dataprovider.WtcInterval;
import com.dpndeveloper.wtc.util.TimeUtil;

/**
 * Adapter to put WtcInterval in a ListView
 * 
 * @author Domenico Pio Novelli
 * 
 */
public class WtcIntervalListAdapter extends SimpleAdapter {

	private static final String	WTC_INTERVAL	= "WtcInterval";

	/**
	 * Constructor
	 * 
	 * @param context
	 *            The context where the View associated with this adapter is
	 *            running
	 * @param intervales
	 *            the list of intervals to adapt
	 */
	public WtcIntervalListAdapter(Context context, List<WtcInterval> intervales) {
		this(context, intervales, false);
	}

	/**
	 * Constructor
	 * 
	 * @param context
	 *            The context where the View associated with this adapter is
	 *            running
	 * @param intervales
	 *            the list of intervals to adapt
	 */
	public WtcIntervalListAdapter(Context context,
			List<WtcInterval> intervales, boolean todayList) {
		super(context, getStrings(intervales, context, todayList),
				R.layout.element_row, new String[] { WTC_INTERVAL },
				new int[] { R.id.textViewRowElement });
	}

	/**
	 * 
	 * @param context
	 *            The context where the View associated with this adapter is
	 *            running
	 * @param intervales
	 *            the list of intervals to adapt
	 * @return the result list of elements
	 */
	private static List<Map<String, Object>> getStrings(
			List<WtcInterval> intervals, Context context, boolean todayList) {
		List<Map<String, Object>> intervalList = new ArrayList<Map<String, Object>>();

		if (intervals != null) {
			Iterator<WtcInterval> iterator = intervals.iterator();
			while (iterator.hasNext()) {
				WtcInterval interval = iterator.next();
				HashMap<String, Object> item = new HashMap<String, Object>();

				String start = TimeUtil.toHoursMinutes(interval.getStartTime());
				String end = "---";

				// If interval has end time, use it
				if (interval.getEndTime() != null) {
					end = TimeUtil.toHoursMinutes(interval.getEndTime());
				} else if (!iterator.hasNext() && todayList) {
					// If the interval doesn't have end time and interval is
					// the last of today
					end = context.getResources().getString(R.string.now);
				}

				item.put(
						WTC_INTERVAL,
						context.getResources().getString(
								R.string.intervalFromTo, start, end));
				intervalList.add(item);
			}
		}
		return intervalList;
	}
}
